package com.cpr.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.cpr.demo.dto.CprUserDto;
import com.cpr.demo.service.CprUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @author Chen Panrui
 * created at 2022/3/18 22:22
 */
@RestController
@RequestMapping("/dsctl")
@Slf4j
@RequiredArgsConstructor
public class DsCtlController {

    private final CprUserService cprUserService;

    @GetMapping("/query/{type}")
    @ResponseBody
    public Object me(@PathVariable String type, @RequestParam("param") Long param){
        if(StrUtil.equals("db1" , type)){
            return cprUserService.findList(CprUserDto.buildParam(param));
        } else if(StrUtil.equals("db0", type)){
            Optional<CprUserDto> one = cprUserService.findOne(param);
            return one.orElseGet(CprUserDto::new);
        }
        return "error";
    }

}
